﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using AppManagement.Repositories;
using AppManagement.Helpers;
using AppManagement.Models;

namespace AppManagement.MainManagers
{
    public partial class FrmDanhSachSinhVienDangKyHocPhan : DevComponents.DotNetBar.Office2007Form
    {
        public EventHandler Handler { get; set; }
        private DataTable DataSource;
        private int MaKhoa;
        private int MaCN;
        private int MaKHDT;
        private int MaCTKHDT;

        public FrmDanhSachSinhVienDangKyHocPhan(int makhoa, int macn, int makhdt, int mactkhdt)
        {
            InitializeComponent();
            InitGridView();
            this.MaKhoa = makhoa;
            this.MaCN = macn;
            this.MaKHDT = makhdt;
            this.MaCTKHDT = mactkhdt;
            InitData();
        }

        public void InitData()
        {
            var khoa = KhoaRepository.SelectByID(this.MaKhoa);
            var chuyennganh = ChuyenNganhRepository.SelectByID(this.MaCN);
            var khdt = KeHoachDaoTaoRepository.SelectByID(this.MaKHDT);
            txtKhoa.Text = khoa.TenKhoa;
            txtChuyenNganh.Text = chuyennganh.TenCN;
            txtKHDT.Text = khdt.TenGoi;

            var ctkhdt = ChiTietKHDTRepository.SelectByID(this.MaCTKHDT);
            txtHocPhan.Text = ctkhdt.HocPhan.TenHP;

            var lstSVDangKy = ChiTietDangKyRepository.SelectByMaCTKHDT(this.MaCTKHDT);

            InitDataSource();

            foreach (var item in lstSVDangKy)
            {
                var lstDiemht = DiemHocTapRepository.SelectByMaCTDK(item.MaCTDK);
                if (lstDiemht.Count > 0)
                {
                    DataSource.Rows.Add(item.MaCTDK, item.PhieuDangKy.SinhVien.MSSV, item.PhieuDangKy.SinhVien.HoTen, lstDiemht[0].DiemQuaTrinh, lstDiemht[0].DiemThi, lstDiemht[0].DiemTongKet);
                }
                else
                {
                    DataSource.Rows.Add(item.MaCTDK, item.PhieuDangKy.SinhVien.MSSV, item.PhieuDangKy.SinhVien.HoTen, string.Empty, string.Empty, string.Empty);
                }
            }
            dtgvData.Columns[0].Visible = false;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
        }

        private void InitGridView()
        {
            DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
            {
                DataPropertyName = "MaCTDK",
                Name = "MaCTDK",
                Visible = false,
                Width = 0
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "MSSV",
                DataPropertyName = "MSSV",
                Name = "MSSV",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) - 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "HoTen",
                DataPropertyName = "HoTen",
                Name = "HoTen",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) + 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemQuaTrinh",
                DataPropertyName = "DiemQuaTrinh",
                Name = "DiemQuaTrinh",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) 
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemThi",
                DataPropertyName = "DiemThi",
                Name = "DiemThi",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) 
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemTongKet",
                DataPropertyName = "DiemTongKet",
                Name = "DiemTongKet",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6))
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemThiLan2",
                DataPropertyName = "DiemThiLan2",
                Name = "DiemThiLan2",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 6)) - 1
            };
            dtgvData.Columns.Add(objColumn);
        }

        public void InitDataSource()
        {
            DataSource = new DataTable();
            DataSource.Columns.Add("MaCTDK");
            DataSource.Columns.Add("MSSV");
            DataSource.Columns.Add("HoTen");
            DataSource.Columns.Add("DiemQuaTrinh");
            DataSource.Columns.Add("DiemThi");
            DataSource.Columns.Add("DiemTongKet");
            DataSource.Columns.Add("DiemThiLan2");
            DataView objTimeSheetDV = new System.Data.DataView(DataSource);
            objTimeSheetDV.AllowNew = false;
            dtgvData.AutoGenerateColumns = false;
            dtgvData.DataSource = objTimeSheetDV;
            dtgvData.ClearSelection();
        }

        private void FrmLapPhieuDangKyHocPhan_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Handler(sender, e);
        }

        private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvData.SelectedRows.Count > 0)
            {
                FrmChamDiemSinhVien frm = new FrmChamDiemSinhVien(int.Parse(dtgvData.SelectedRows[0].Cells["MaCTDK"].Value.ToString()));
                frm.Handler += CapNhatDiem;
                frm.ShowDialog();
            }
        }

        private void CapNhatDiem(object sender, EventArgs e)
        {
            var data = (TransferSimpleDataEvent)e;
            var comp = data.Data.Split(new char[] { '#' });
            var selectedItem = dtgvData.SelectedRows[0];
            selectedItem.Cells["DiemQuaTrinh"].Value = comp[0];
            selectedItem.Cells["DiemThi"].Value = comp[1];
            selectedItem.Cells["DiemTongKet"].Value = comp[2];
            selectedItem.Cells["DiemThiLan2"].Value = comp[3];
        }

        private void btnInBangDiem_Click(object sender, EventArgs e)
        {

        }
    }
}